#coding=utf-8
# !/usr/bin/python
# -*- coding: utf-8 -*-

from com import * 
import struct
from PySide6.QtGui import QPalette, QColor


class Devcfg:
    def __init__(self, ui):
        self.ui = ui
        self.com = ui.com
        self.wl_wirte =[self.ui.lineEdit_175, self.ui.lineEdit_169,
                        self.ui.lineEdit_179, self.ui.lineEdit_181,
                        self.ui.lineEdit_173, self.ui.lineEdit_178,
                        self.ui.lineEdit_172, self.ui.lineEdit_180,
                        self.ui.lineEdit_170, self.ui.lineEdit_176,
                        self.ui.lineEdit_177, self.ui.lineEdit_171,]
        
        self.wl_read = [self.ui.lineEdit_182, self.ui.lineEdit_174,
                        self.ui.lineEdit_183, self.ui.lineEdit_184,
                        self.ui.lineEdit_185, self.ui.lineEdit_186,
                        self.ui.lineEdit_187, self.ui.lineEdit_188,
                        self.ui.lineEdit_189, self.ui.lineEdit_190,
                        self.ui.lineEdit_191, self.ui.lineEdit_192,]
        
        self.val = [i for i in range( len(self.wl_wirte) )]
        self.readval = [i for i in range( len(self.wl_wirte) )]

        self.setupui()
        self.UISend_button1 = self.ui.pushButton_4 # 读取按钮
        self.UISend_button2 = self.ui.pushButton_5 # 写入按钮
        self.UISend_button3 = self.ui.pushButton_2 # 载入默认
        self.UISend_button4 = self.ui.pushButton_3 # 载入读取值
        self.UISend_button5 = self.ui.pushButton_15 # 保存默认
        
        self.UISend_button1.clicked.connect(self.ui_pushButton1_cb)
        self.UISend_button2.clicked.connect(self.ui_pushButton2_cb)
        self.UISend_button3.clicked.connect(self.load_default)
        self.UISend_button4.clicked.connect(self.wirte_default)
        self.UISend_button5.clicked.connect(self.load_readval)
        
          
    def setupui(self):
        self.load_default()
        
    def load_ui_cb(self):
        for i in range( len(self.wl_wirte) ):
            self.wl_wirte[i].setText("{:d}".format(self.val[i]))
 
    def load_default(self):
        for i in range( len(self.wl_wirte) ):
            self.val[i] = self.ui.cfg.data['devcfg'][i]
        self.load_ui_cb()
        
    def wirte_default(self):
        for i in range( len(self.wl_wirte) ):
            self.val[i] = str_to_int_safe(self.wl_wirte[i].text())
            self.ui.cfg.data['devcfg'][i] = self.val[i]
        self.ui.cfg.save()
   
    def load_readval(self):   
        for i in range( len(self.wl_wirte) ):
            self.val[i] = self.readval[i]
        self.load_ui_cb()
        
    def ui_pushButton1_cb(self):
        self.ui_cb_send_readmsg()

    def ui_pushButton2_cb(self):
        for i in range( len(self.wl_wirte) ):
            self.val[i] = str_to_int_safe(self.wl_wirte[i].text())
        self.ui_cb_send_writemsg()
        

    def ui_cb_send_readmsg(self):
        if self.com.serdev.ser.isOpen():
            array = self.com.pack_frame_head(0x24, 0x65)
            #self.pack_matain_msg(array)
            self.com.pack_frame_tail(array)
            self.com.serdev.ser.write(array)
            # print(' '.join(f'{c:02x}' for c in array))
        else:
            pass  
            
    def ui_cb_send_writemsg(self):
        if self.com.serdev.ser.isOpen():
            array = self.com.pack_frame_head(0x24, 0x35)
            self.pack_wirtemsg(array)
            self.com.pack_frame_tail(array)
            self.com.serdev.ser.write(array)
            print(' '.join(f'{c:02x}' for c in array))
        else:
            pass
            
    def pack_wirtemsg(self, array):
        array[10] = self.val[0]
        array[11] = self.val[1]
        array[12] = self.val[2]
        array[14] = self.val[3]
        array[15] = self.val[4] & 0xff
        array[16] = (self.val[4] >> 8)& 0xff    
        array[17] = self.val[5] & 0xff
        array[18] = (self.val[5] >> 8)& 0xff
        array[19] = self.val[6] & 0xff
        array[20] = (self.val[6] >> 8)& 0xff
        array[21] = self.val[7] & 0xff
        array[22] = (self.val[7] >> 8)& 0xff
        array[23] = self.val[8] & 0xff
        array[24] = (self.val[8] >> 8)& 0xff 
        array[25] = self.val[9] & 0xff
        array[26] = (self.val[9] >> 8)& 0xff
        array[27] = self.val[10] & 0xff
        array[28] = (self.val[10] >> 8)& 0xff
        array[29] = self.val[11] & 0xff
        array[30] = (self.val[11] >> 8)& 0xff
        
        
    def decode(self, a):
        self.readval[0] = struct.unpack('<B', a[10:11])[0] # 10 产品气置换时间，单位1s
        self.readval[1] = struct.unpack('<B', a[11:12])[0] # 11 产品气置换供氧阀开度 
        self.readval[2] = struct.unpack('<B', a[12:13])[0] # 12 氧分压校准供氧阀开度
        self.readval[3] = struct.unpack('<B', a[14:15])[0] # 14 调试参数使能
        
        self.readval[4] = struct.unpack('<h', a[15:17])[0] # 15 调试参数1 
        self.readval[5] = struct.unpack('<h', a[17:19])[0] # 17 调试参数2
        self.readval[6] = struct.unpack('<h', a[19:21])[0] # 19 调试参数3 
        self.readval[7] = struct.unpack('<h', a[21:23])[0] # 21 调试参数4
        self.readval[8] = struct.unpack('<h', a[23:25])[0] # 23 调试参数5
        self.readval[9] = struct.unpack('<h', a[25:27])[0] # 25 调试参数6 
        self.readval[10] = struct.unpack('<h', a[27:29])[0] # 27 调试参数7
        self.readval[11] = struct.unpack('<h', a[29:31])[0] # 29 调试参数8
        
        for i in range( len(self.wl_read) ):
            self.wl_read[i].setText("{:d}".format(self.readval[i]))
        

     



           

